home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / visual / perl.exe / {app} / Webroot / cgi-bin / graffiti.pl < prev    next >
Encoding:
Perl Script  |  2003-01-11  |  3.1 KB  |  110 lines

  1. #!perl
  2.  
  3. # Keep a growing list of phrases from the user.
  4.  
  5. # CONSTANTS
  6. $STATE_DIR = ".";  # must be writable by 'nobody'
  7.  
  8. use CGI qw/:standard/;
  9.  
  10. $session_key = path_info();
  11. $session_key =~ s|^/||;             # get rid of the initial slash
  12.  
  13. # If no valid session key has been provided, then we
  14. # generate one, tack it on to the end of our URL as
  15. # additional path information, and redirect the user
  16. # to this new location.
  17. unless (valid($session_key)) {
  18.     $session_key = generate_session_key();
  19.     print redirect(url() . "/$session_key");
  20.     exit 0;
  21. }
  22.  
  23. # pull out our current CGI state variables to prevent them from being
  24. # overwritten when we restore our previous state.
  25. @new_items = param('item');
  26. $action = param('action');
  27.  
  28. fetch_old_state($session_key);
  29.  
  30. # Add the new item(s) to the old list of items
  31. if ($action eq 'ADD') {
  32.     @old_items = param('item');
  33.     param('item',@old_items,@new_items);
  34. } elsif ($action eq 'CLEAR') {
  35.     Delete('item');
  36. }
  37.  
  38. # Save the new list to disk
  39. save_state($session_key);
  40.  
  41. # Now, at last, generate something for the use to look at.
  42. print header,
  43.     start_html("The growing list"),
  44.     <<END;
  45. <h1>The Growing List</h1>
  46. Type a short phrase into the text field below.  Press <i>ADD</i>,
  47. to append it to the history of the phrases that you've typed.  The
  48. list is maintained on disk at the server end, so it won't get out of
  49. order if you press the "back" button.  Press <i>CLEAR</i> to clear the
  50. list and start fresh.  Bookmark this page to come back to the list later.
  51. END
  52.      ;
  53.  
  54. print start_form,
  55.     textfield(-name=>'item',-default=>'',-size=>50,-override=>1),p(),
  56.     submit(-name=>'action',-value=>'CLEAR'),
  57.     submit(-name=>'action',-value=>'ADD'),
  58.     end_form,
  59.     hr,
  60.     h2('Current list');
  61.  
  62. if (param('item')) {
  63.     my @items = param('item');
  64.     print ol(li(\@items));
  65. } else {
  66.     print em('Empty');
  67. }
  68. print <<END;
  69. <hr>
  70. <a href="../../source.html">Code examples</a></address>
  71. END
  72.     ;
  73. print end_html;
  74.  
  75. # Silly technique: we generate a session key from a random number
  76. # generator, and keep calling until we find a unique one.
  77. sub generate_session_key {
  78.     my $key;
  79.     do {
  80.         $key = int(rand(1000000));
  81.     } until (! -e "$STATE_DIR/$key");
  82.     return $key;
  83. }
  84.  
  85. # make sure the session ID passed to us is a valid one by
  86. # looking for a numeric-only string
  87. sub valid {
  88.     my $key = shift;
  89.     return $key=~/^\d+$/;
  90. }
  91.  
  92. # Open the existing file, if any, and read the current state from it.
  93. # We use the CGI object here, because it's straightforward to do.
  94. # We don't check for success of the open() call, because if there is
  95. # no file yet, the new CGI(FILEHANDLE) call will return an empty
  96. # parameter list, which is exactly what we want.
  97. sub fetch_old_state {
  98.     my $session_key = shift;
  99.     open(SAVEDSTATE,"$STATE_DIR/$session_key") || return;
  100.     restore_parameters(SAVEDSTATE);
  101.     close SAVEDSTATE;
  102. }
  103.  
  104. sub save_state {
  105.     my($session_key) = @_;
  106.     open(SAVEDSTATE,">$STATE_DIR/$session_key") ||
  107.         die "Failed opening session state file: $!";
  108.     save_parameters(SAVEDSTATE);
  109.     close SAVEDSTATE;
  110. }